﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-9-13
Description:CustomerGroup三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class CustomerGroupService
	{
		#region 增加一条数据
		public static int AddCustomerGroup(CustomerGroup customerGroup)
		{
			string sql =
			"INSERT INTO CustomerGroup(GroupName,GroupMemo) "+
			"VALUES (@GroupName,@GroupMemo)";
			
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
						
					new SQLiteParameter("@GroupName",customerGroup.GroupName),
						
					new SQLiteParameter("@GroupMemo",customerGroup.GroupMemo)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteCustomerGroupById(long id)	
		{
			string sql="DELETE From  CustomerGroup  Where id=@id";
			 SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifyCustomerGroupByid(CustomerGroup customerGroup)
		{
		string sql ="UPDATE "+"CustomerGroup"+" set "+
					"GroupName=@GroupName, "+
					"GroupMemo=@GroupMemo "+ 
				" Where id=@id";
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id",customerGroup.Id),
					new SQLiteParameter("@GroupName",customerGroup.GroupName),
					new SQLiteParameter("@GroupMemo",customerGroup.GroupMemo)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<CustomerGroup> GetAllCustomerGroups()
		{
			string sqlAll = "SELECT * FROM CustomerGroup";
			return GetCustomerGroupsBySql(sqlAll);
		}
		//以标识列获得对象
		public static CustomerGroup GetCustomerGroupByid(object id)
		{
			string sql = "SELECT * FROM "+"CustomerGroup"+" WHERE id = @id";
			
            using (SQLiteDataReader reader = DBHelper.GetReader(sql, new SQLiteParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					CustomerGroup customerGroup = new CustomerGroup();
					customerGroup.Id=Convert.ToInt32(reader["id"]);
					customerGroup.GroupName=Convert.ToString(reader["GroupName"]);
					customerGroup.GroupMemo=Convert.ToString(reader["GroupMemo"]);
					reader.Close();
					
					return customerGroup;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<CustomerGroup> GetCustomerGroupsBySql(string safeSql)
		{
			List<CustomerGroup> list = new List<CustomerGroup>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					CustomerGroup customerGroup = new CustomerGroup();
					customerGroup.Id=Convert.ToInt32(row["id"]);
					customerGroup.GroupName=Convert.ToString(row["GroupName"]);
					customerGroup.GroupMemo=Convert.ToString(row["GroupMemo"]);
					list.Add(customerGroup);
				}
				return list;
			}
             
		}
		private static IList<CustomerGroup> GetCustomerGroupsBySql( string sql, params SQLiteParameter[] values )
		{
			List<CustomerGroup> list = new List<CustomerGroup>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					CustomerGroup customerGroup = new CustomerGroup();
					customerGroup.Id=Convert.ToInt32(row["id"]);
					customerGroup.GroupName=Convert.ToString(row["GroupName"]);
					customerGroup.GroupMemo=Convert.ToString(row["GroupMemo"]);
					list.Add(customerGroup);
				}
				return list;
			}
            
			 
		}
		#endregion
		 
	}
}

